package com.example.szh.mvp.view.user;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.szh.R;
import com.example.szh.adapter.HomePageVpAdapter;
import com.example.szh.db.DBManager;
import com.example.szh.entity.UserEntity;
import com.example.szh.utils.LoadImage;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.List;

public class HomePageActivity extends AppCompatActivity {

    private ImageView menuHeadImg;
    private TextView homepageName;
    private SharedPreferences isLogin;
    private SlidingTabLayout homepageNameTab;
    private ViewPager homepageVp;
    private HomePageVpAdapter adapter;
    private ImageView homepageBackBnt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        initView();
        isLogin = getSharedPreferences("isLogin", Context.MODE_PRIVATE);
        boolean is = isLogin.getBoolean("is", false);
        if (is) {
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
                LoadImage.loadImg(list.get(0).getUser_img(), menuHeadImg);
                homepageName.setText(list.get(0).getUser_name());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        adapter = new HomePageVpAdapter(getSupportFragmentManager());
        homepageVp.setAdapter(adapter);
        homepageNameTab.setViewPager(homepageVp);
        homepageBackBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        menuHeadImg = (ImageView) findViewById(R.id.menu_head_img);
        homepageName = (TextView) findViewById(R.id.homepage_name);
        homepageNameTab = (SlidingTabLayout) findViewById(R.id.homepage_name_tab);
        homepageVp = (ViewPager) findViewById(R.id.homepage_vp);
        homepageBackBnt = (ImageView) findViewById(R.id.homepage_backBnt);
    }
}